package com.easy.controller;

import com.easy.bean.Guests;
import com.easy.common.CommonResult;
import com.easy.dao.IGuestsDao;
import com.easy.service.IGuests;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class GuestsController {
    @Autowired
    IGuestsDao dao;

    @Autowired
    IGuests easyS;

    //查询
    @GetMapping("searchguests")
    public CommonResult getSearchGuests(Guests guests, Page page){
        List<Guests> list=easyS.getSearchEmployess(guests,page);
        int count=easyS.getCount(guests);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);

        return CommonResult.success(map);
    }



    //添加
    @PostMapping("guestsjson")
    public CommonResult addGuestsJson(@RequestBody Guests guests){
       easyS.addGuests(guests);
       guests=easyS.getGuests(guests.getId());
        return CommonResult.success(guests);
    }
//    @PostMapping("guests")
//    public String addGuest( Guests guests){
//        dao.addGuests(guests);
//        return "success";
//    }



    @GetMapping("guests")
    public CommonResult getSearchGuests(Guests guests){
        List<Guests> list=dao.getSearchGuests(guests);
        return CommonResult.success(list);
    }
    //删除
    @DeleteMapping("guests/{id}")
    public CommonResult delGuests(@PathVariable int id){
        int count= easyS.del(id);
        if (count>0){
            return CommonResult.success(count);
        }else{
            return CommonResult.fail();
        }
    }

//    @DeleteMapping("guest/{id}")
//    public String del(@PathVariable int id){
//        dao.delGuests(id);
//        return "success";
//    }


    //修改
    @PutMapping("guestsjson")
    public CommonResult editGuestsJson(@RequestBody Guests guests){
        int count=easyS.editGuests(guests);

        guests=easyS.getGuests(guests.getId());
        return CommonResult.success(guests);
    }

//    @PutMapping("guests")
//    public String editGuests(Guests guests){
//        dao.editGuests(guests);
//        return "success";
//    }

}
